from collections import OrderedDict

from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response


class StandardResultPagination(PageNumberPagination):
    "自定义分页类"
    #指定页容量
    page_size = 1
    #指定获取分页数据时‘页容量’的参数名称
    page_size_query_param = 'pagesize'
    #指定最大页容量
    max_page_size = 20

    def get_paginated_response(self, data):
        "重写父类方法，自定义分页相应数据格式"
        return Response(OrderedDict([
            ('count',self.page.paginator.count),
            ('lists',data),
            ('page',self.page.number),
            ('pages',self.page.paginator.num_pages),
            ('pagesize',self.get_page_size(self.request))
        ]))